package controllers.utils;

import java.io.Serializable;
import java.util.Locale;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
import javax.faces.context.FacesContext;

@SessionScoped
@Named(value="localeChanger")
public class LocaleChanger implements Serializable {
    private String locale = "no";

    public LocaleChanger() {}

    public String changeLocale(String languageCode){
        FacesContext context = FacesContext.getCurrentInstance();
        context.getViewRoot().setLocale(new Locale(languageCode));
        locale=languageCode;
        return null;
    }

    public String getLocale() {
        if(locale==null){
            FacesContext context = FacesContext.getCurrentInstance();
            System.out.println("Current locale: " + context.getViewRoot().getLocale().getDisplayCountry());
            setLocale(context.getViewRoot().getLocale().getDisplayCountry());
        }
        return locale;
    }

    public String changeLocaleTo(String langCode){
        this.locale = langCode;
        return null;
    }

    public void setLocale(String locale) {
        this.locale = locale;
    }


}